package com.medical.data.service.impl;

import com.medical.data.domain.Patient;
import com.medical.data.domain.SendMessage;
import com.medical.data.domain.User;
import com.medical.data.exception.MyException;
import com.medical.data.service.*;
import com.alibaba.alicloud.sms.SmsServiceImpl;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.mns.common.ClientException;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.medical.data.utils.ResultCode;
import com.mysql.cj.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class SmsSendServiceImpl implements SmsSendService {

    @Autowired
    private SmsServiceImpl smsService;

    @Autowired
    private TokenSerivce tokenSerivce;

    @Autowired
    private HospitalInformationService hospitalInformationService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private FollowPatientService followPatientService;

    @Autowired
    private PatientService patientService;

    @Autowired
    private UserPermissionService userPermissionService;

    public void testSendSms2(String patientDataId) throws ClientException, com.aliyuncs.exceptions.ClientException {
        String patientId = followPatientService.getPatientById(patientDataId).getPatientId();
        userPermissionService.getFollowUpPermission(patientService.getProjectIdByPatientId(patientId));
        if (followPatientService.getRemindById(patientDataId)) {
            throw new MyException(ResultCode.ERROR.getCode(), "该患者本次随访已提醒过，不能重复提醒");
        }
        //更新提醒字段
        followPatientService.updateRemindStatus(patientDataId);
        User user = tokenSerivce.getUser();
        SendMessage sendMessage = new SendMessage();
        sendMessage.setDoctorName(user.getNickName());
        sendMessage.setPhone(user.getPhone());
        sendMessage.setHospitalName(hospitalInformationService.getNameById(user.getHsId()));
        sendMessage.setKsName(departmentService.getDepartmentById(user.getDepartmentId()));
        sendMessage.setName(followPatientService.getPatientById(patientDataId).getName());
        sendMessage.setTime(followPatientService.getPatientById(patientDataId).getFollowUpTime());
        //        SmsRequest
        // 组装请求对象-具体描述见控制台-文档部分内容
        JSONObject json = new JSONObject();
        // 将 SendMessage 对象的属性逐个添加到 JSONObject 中
        json.put("doctorName", sendMessage.getDoctorName());
        json.put("phone", sendMessage.getPhone());
        json.put("hospitalName", hospitalInformationService.getNameById(user.getHsId()));
        json.put("ksName", departmentService.getDepartmentById(user.getDepartmentId()));
        json.put("name", followPatientService.getPatientById(patientDataId).getName());
        json.put("time", followPatientService.getPatientById(patientDataId).getFollowUpTime());
        String jsonStr = json.toJSONString();
        com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest request = new com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest();
        // 必填:待发送手机号
        request.setPhoneNumbers(followPatientService.getPatientById(patientDataId).getPhone());//19155605345
        // 必填:短信签名-可在短信控制台中找到
        request.setSignName("医疗数据收集平台");
        // 必填:短信模板-可在短信控制台中找到
        request.setTemplateCode("SMS_465322748");//SMS_465337699,SMS_465322748
        // 可选:模板中的变量替换JSON串,如模板内容您的验证码为${code}"时,此处的值为
        System.out.println(jsonStr);
        request.setTemplateParam(jsonStr);
        SendSmsResponse response = smsService.sendSmsRequest(request);
        System.out.println("短信接口返回的数据----------------");
        System.out.println("Code=" + response.getCode());
        System.out.println("Message=" + response.getMessage());
        System.out.println("RequestId=" + response.getRequestId());
        System.out.println("BizId=" + response.getBizId());//消息回执id
    }

    @Override
    public void sendSmsToPatient(String patientId, String text) throws ClientException, com.aliyuncs.exceptions.ClientException {
        //        SmsRequest
        userPermissionService.getFollowUpPermission(patientService.getProjectIdByPatientId(patientId));
        User user = tokenSerivce.getUser();
        Patient patient = patientService.getPatientById(patientId);
        // 组装请求对象-具体描述见控制台-文档部分内容
        JSONObject json = new JSONObject();
        // 将 SendMessage 对象的属性逐个添加到 JSONObject 中
        json.put("patientName", patient.getPatientName());
        json.put("doctorName", user.getNickName());
        json.put("text", text);
        String jsonStr = json.toJSONString();
        com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest request = new com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest();
        // 必填:待发送手机号
        request.setPhoneNumbers(patient.getPatientPhone());//   "19155605345"
        // 必填:短信签名-可在短信控制台中找到
        request.setSignName("医疗数据收集平台");
        // 必填:短信模板-可在短信控制台中找到
        request.setTemplateCode("SMS_465413016");//SMS_465337699,SMS_465322748
        // 可选:模板中的变量替换JSON串,如模板内容您的验证码为${code}"时,此处的值为
        request.setTemplateParam(jsonStr);
        SendSmsResponse response = smsService.sendSmsRequest(request);
        System.out.println("短信接口返回的数据----------------");
        System.out.println("Code=" + response.getCode());
        System.out.println("Message=" + response.getMessage());
        System.out.println("RequestId=" + response.getRequestId());
        System.out.println("BizId=" + response.getBizId());//消息回执id
    }
}
